/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.acegi;

import com.blandware.atleap.webapp.util.core.WebappConstants;
import org.acegisecurity.Authentication;
import org.acegisecurity.AuthenticationException;
import org.acegisecurity.ui.webapp.AuthenticationProcessingFilter;
import org.apache.commons.validator.GenericValidator;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>This class provides abilities to redirect into url from saved request and
 * remember the target URI.</p>
 * <p><a href="CustomAuthenticationProcessingFilter.java.html"><i>View Source</i></a></p>
 *
 * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
 * @version $Revision: 1.7 $ $Date: 2008/06/23 14:58:03 $
 * @web.filter name="authenticationProcessingFilter"
 */
public class CustomAuthenticationProcessingFilter extends AuthenticationProcessingFilter {

    /**
     * Name of parameter through which URI is passed.
     */
    public static final String FORM_URI_KEY = "j_uri";

    /**
     * Overridden method in order to put j_uri into request attribute,
     * if exists in parameters.
     *
     * @param request   Request
     * @return Authentication result
     * @throws AuthenticationException
     */
    public Authentication attemptAuthentication(HttpServletRequest request) throws AuthenticationException {
        if (!GenericValidator.isBlankOrNull(request.getParameter(FORM_URI_KEY))) {
            request.getSession().setAttribute(WebappConstants.AUTH_TARGET_URL_KEY,
                    request.getParameter(FORM_URI_KEY));
        }
        return super.attemptAuthentication(request);
    }
}
